package com.coocaa.launcher.statusplugins.externalstorege;

import android.content.Context;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.coocaa.launcher.framework.launcherhost.statusbar.StatusBarPluginView;
import com.coocaa.launcher.statusplugins.externalstorege.StatusPluginExternalStorage.ExternalStorageChange;
import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;

public class StatusPluginExternalStorageView extends StatusBarPluginView implements
        ExternalStorageChange {
    private ImageView usb = null, sd = null;
    private LinearLayout root = null;

    public static enum ESTORAGETYPE {
        SDCARD, USB
    }

    public StatusPluginExternalStorageView(Context context, String name) {
        super(context, name);
        root = new LinearLayout(mContext);
        root.setOrientation(LinearLayout.HORIZONTAL);
        root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
        usb = new ImageView(mContext);
        usb.setBackgroundResource(R.drawable.sp_storage);
        sd = new ImageView(mContext);
        sd.setBackgroundResource(R.drawable.sp_sdcard);
        addView(root);
        // iv.setBackgroundResource(R.drawable.focus_bg);
        // addView(iv);
    }

    @Override
    public int getConfigGravity() {
        return Gravity.RIGHT;
    }

    @Override
    public void onChange(final ESTORAGETYPE type, final boolean bIn) {
        LogUtils.d("Storage", "type:" + type.toString() + "  bIn:" + bIn);
        if (bIn) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    LayoutParams ivParams = new LayoutParams(Div(46), Div(37));
                    switch (type) {
                        case SDCARD:
                            if (sd.getParent() != null) {
                            } else {
                                root.addView(sd, ivParams);
                            }
                            break;
                        case USB:
                            if (usb.getParent() != null) {
                            } else {
                                root.addView(usb, ivParams);
                            }
                            break;
                        default:
                            break;
                    }
                }
            });
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    switch (type) {
                        case SDCARD:
                            root.removeView(sd);
                            break;
                        case USB:
                            root.removeView(usb);
                            break;
                        default:
                            break;
                    }
                }
            });
        }
    }
}
